package com.otp.service.impl;

import com.otp.pojo.entity.Message;
import com.otp.pojo.entity.Reply;
import com.otp.exception.BusinessException;
import com.otp.repository.MessageRepository;
import com.otp.repository.ReplyRepository;
import com.otp.pojo.request.message.*;
import com.otp.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageRepository messageRepository;

    @Autowired
    private ReplyRepository replyRepository;

    @Override
    public List<Message> list() {
        return messageRepository.findAll();
    }

    @Override
    public Message getById(Long id) {
        return messageRepository.findById(id)
                .orElseThrow(() -> new BusinessException("留言不存在"));
    }

    @Override
    public Message create(MessageCreateRequest request) {
        if (request.getTitle() == null || request.getTitle().trim().isEmpty()) {
            throw new BusinessException("标题不能为空");
        }
        if (request.getContent() == null || request.getContent().trim().isEmpty()) {
            throw new BusinessException("内容不能为空");
        }
        Message message = new Message();
        message.setTitle(request.getTitle());
        message.setContent(request.getContent());
        message.setStudentId(request.getStudentId());
        message.setHasUnreadReply(false);
        message.setCreateTime(LocalDateTime.now());
        return messageRepository.save(message);
    }

    @Override
    public Message update(MessageUpdateRequest request) {
        Message message = getById(request.getId());
        message.setStudentId(request.getStudentId());
        message.setTitle(request.getTitle());
        message.setContent(request.getContent());
        return messageRepository.save(message);
    }

    @Override
    public void delete(Long id) {
        Message message = getById(id);
        replyRepository.deleteAll(replyRepository.findByMessageId(id));
        messageRepository.delete(message);
    }

    @Override
    public List<Reply> getReplies(Long messageId) {
        getById(messageId); // 验证留言存在
        return replyRepository.findByMessageId(messageId);
    }

    @Override
    public Reply createReply(ReplyCreateRequest request) {
        Message message = getById(request.getMessageId());
        if (request.getContent() == null || request.getContent().trim().isEmpty()) {
            throw new BusinessException("回复内容不能为空");
        }
        Reply reply = new Reply();
        reply.setMessageId(request.getMessageId());
        reply.setTeacherId(request.getTeacherId());
        reply.setContent(request.getContent());
        reply.setCreateTime(LocalDateTime.now());
        message.setHasUnreadReply(true);
        messageRepository.save(message);
        return replyRepository.save(reply);
    }

    @Override
    public void deleteReply(Long replyId) {
        Reply reply = replyRepository.findById(replyId)
                .orElseThrow(() -> new BusinessException("回复不存在"));
        replyRepository.delete(reply);
    }

    @Override
    public List<Message> getNotifications(Long studentId) {
        return messageRepository.findByHasUnreadReplyTrueAndStudentId(studentId);
    }

    @Override
    public Message markAsRead(Long messageId) {
        Message message = getById(messageId);
        message.setHasUnreadReply(false);
        return messageRepository.save(message);
    }
}